package org.pt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.pt.components.Response;
import org.pt.mapper.CommentMapper;
import org.pt.mapper.LikeMapper;
import org.pt.mapper.PostMapper;
import org.pt.model.Comment;
import org.pt.model.Like;
import org.pt.model.Post;
import org.pt.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private LikeMapper likeMapper;

    @Override
    public Response<List<Post>> getAllPosts() {
        try {
            QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
            // 按照置顶优先，然后按创建时间倒序排列
            queryWrapper.orderByDesc("is_sticky", "create_time");
            List<Post> posts = postMapper.selectList(queryWrapper);
            return Response.success(posts);
        } catch (Exception e) {
            log.error("查询所有帖子失败", e);
            return Response.error("查询帖子失败", 500);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Response<String> stickyPost(Long postId) {
        try {
            Post post = postMapper.selectById(postId);
            if (post == null) {
                return Response.error("帖子不存在", 404);
            }
            
            if (Boolean.TRUE.equals(post.getIsSticky())) {
                return Response.error("帖子已经是置顶状态", 400);
            }
            
            post.setIsSticky(true);
            int result = postMapper.updateById(post);
            
            if (result > 0) {
                log.info("帖子置顶成功，帖子ID: {}", postId);
                return Response.success("帖子置顶成功");
            } else {
                return Response.error("帖子置顶失败", 500);
            }
        } catch (Exception e) {
            log.error("帖子置顶失败，帖子ID: {}", postId, e);
            return Response.error("帖子置顶失败", 500);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Response<String> unstickyPost(Long postId) {
        try {
            Post post = postMapper.selectById(postId);
            if (post == null) {
                return Response.error("帖子不存在", 404);
            }
            
            if (Boolean.FALSE.equals(post.getIsSticky()) || post.getIsSticky() == null) {
                return Response.error("帖子不是置顶状态", 400);
            }
            
            post.setIsSticky(false);
            int result = postMapper.updateById(post);
            
            if (result > 0) {
                log.info("取消帖子置顶成功，帖子ID: {}", postId);
                return Response.success("取消帖子置顶成功");
            } else {
                return Response.error("取消帖子置顶失败", 500);
            }
        } catch (Exception e) {
            log.error("取消帖子置顶失败，帖子ID: {}", postId, e);
            return Response.error("取消帖子置顶失败", 500);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Response<String> deletePost(Long postId) {
        try {
            log.info("开始删除帖子，帖子ID: {}", postId);
            
            Post post = postMapper.selectById(postId);
            if (post == null) {
                log.warn("帖子不存在，帖子ID: {}", postId);
                return Response.error("帖子不存在", 404);
            }
            
            log.info("查询到帖子信息: {}", post);
            
            // 删除关联的评论
            QueryWrapper<Comment> commentWrapper = new QueryWrapper<>();
            commentWrapper.eq("post_id", postId);
            commentMapper.delete(commentWrapper);
            log.info("删除帖子关联的评论完成");
            
            // 删除关联的点赞
            QueryWrapper<Like> likeWrapper = new QueryWrapper<>();
            likeWrapper.eq("post_id", postId);
            likeMapper.delete(likeWrapper);
            log.info("删除帖子关联的点赞完成");
            
            // 删除帖子
            int result = postMapper.deleteById(postId);
            
            if (result > 0) {
                log.info("删除帖子成功，帖子ID: {}", postId);
                return Response.success("删除帖子成功");
            } else {
                log.error("删除帖子失败，影响行数为0，帖子ID: {}", postId);
                return Response.error("删除帖子失败", 500);
            }
        } catch (Exception e) {
            log.error("删除帖子时发生异常，帖子ID: {}", postId, e);
            return Response.error("删除帖子失败: " + e.getMessage(), 500);
        }
    }
} 